package com.zxl.manager.privacy.utils.track.crash;

import android.app.Service;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.IBinder;
import android.text.TextUtils;

import com.zxl.manager.privacy.utils.ApplicationHelper;
import com.zxl.manager.privacy.utils.encrypt.MD5;
import com.zxl.manager.privacy.utils.track.MobclickAgentHelper;
import com.zxl.manager.privacy.utils.utils.FileUtil;
import com.zxl.manager.privacy.utils.utils.SpUtils;

import java.util.Iterator;
import java.util.Map;

/**
 * Created by Administrator on 2016/8/29.
 */
public class CrashReportService extends Service implements Runnable {

    private static final String ACTION_NOW_CRASH = "action_now_crash";

    private static final String DATA_KEY = "data_key";

    private static final String DATA_VALUE = "data_value";

    @Override
    public void onCreate() {
        super.onCreate();
        new Thread(this).start();
    }

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        if (null != intent) {
            String action = intent.getAction();
            if (ACTION_NOW_CRASH.equals(action)) {
                String key = intent.getStringExtra(DATA_KEY);
                String value = intent.getStringExtra(DATA_VALUE);
                String content = FileUtil.readFileToString(value);
                if (!TextUtils.isEmpty(content)) {
                    MobclickAgentHelper.reportError(this, content);
                    SpUtils.getSharedPreferences("crash_report").edit().remove(key).commit();
                }
                sendBroadcast(new Intent("action.db.write.change"));
                stopSelf();
            }
        }
        return super.onStartCommand(intent, flags, startId);
    }

    @Override
    public void run() {
        try {
            SharedPreferences preferences = SpUtils.getSharedPreferences("crash_report");
            Map<String, ?> all = preferences.getAll();
            Iterator<String> iterator = all.keySet().iterator();
            while (iterator.hasNext()) {
                String next = iterator.next();
                String path = (String) all.get(next);
                String content = FileUtil.readFileToString(path);
                MobclickAgentHelper.reportError(this, content);
                preferences.edit().remove(next).commit();
            }
            stopSelf();
        } catch (Throwable e) {
            e.printStackTrace();
        }
    }

    public static void saveCrashRecord(String reportFile) {
        String key = MD5.encode(reportFile);
        SpUtils.getSharedPreferences("crash_report").edit().putString(key, reportFile);

        Intent intent = new Intent(ApplicationHelper.getContext(), CrashReportService.class);
        intent.setAction(ACTION_NOW_CRASH);
        intent.putExtra(DATA_KEY, key);
        intent.putExtra(DATA_VALUE, reportFile);
        ApplicationHelper.getContext().startService(intent);

    }
}
